<?php

namespace App\Http\Controllers;
use App\Msg;
use DB;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class MsgController extends Controller
{
    //展示留言板
   public function index($keyword=""){
       $msg=new \App\Msg();
       if($_GET){
           @$keyword=$_GET['keyword'];
           $list=$msg->select($keyword);
       }else{
           $list=DB::table('msgs')->paginate(3);
       }
       return view('msg.index',['msg'=>$list,'keyword'=>$keyword]);
   }
    //展示添加表单
    public function add(){
        return view('msg.add');
    }
    //添加数据
    public function addPost(){
        $msg=new \App\Msg();
        if($msg->add()){
            return redirect('/msg/index');
        }
    }
    //修改留言
    public function up($id){
        $msg=new \App\Msg();
        if(empty($_POST)){
            $row=$msg::find($id);
            return view('msg.up',['row'=>$row]);
        }else{
            $list=$msg->show($id);
            if(!empty($list->img)) {
                unlink($list->img);
            }
            if($msg->up($id)){
                return redirect('/msg/index');
            }
        }
    }
    //删除留言
    public function del($id){
        $msg=new \App\Msg();
        $list=$msg->show($id);
        if(!empty($list->img)){
            unlink($list->img);
        }
        if($msg->del($id)){
            return redirect('/msg/index');
        }
    }
    
    
}
